//
//  AlertImageView.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/13.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "AlertImageView.h"

@interface AlertImageView()
@property (nonatomic ,strong) UIView *dropView;//背景
@property (nonatomic ,strong) UIWindow *window;
@property (nonatomic, strong) UIView *disaplayView;

@property (nonatomic, strong ) UIButton *confirmBtn;
@end
@implementation AlertImageView

- (instancetype)initWithTheme:(NSString *)imageName
                   firstTitle:(NSString *)firstTitle
                  secondTitle:(NSString *)secondTitle
                  oprationNum:(NSInteger)operationNum{
    if (self = [super init]) {
        [self loadSubviewsOnAlertTintWithTheme:imageName firstTitle:firstTitle secondTitle:secondTitle oprationNum:operationNum];
    }
    return self;
}

- (void)loadSubviewsOnAlertTintWithTheme:(NSString *)imageName
                              firstTitle:(NSString *)firstTitle
                             secondTitle:(NSString *)secondTitle
                             oprationNum:(NSInteger)operationNum{
    self.backgroundColor = [UIColor clearColor];
    self.window = [UIApplication sharedApplication].keyWindow;
    [self.window addSubview:self.dropView];
    [self.window addSubview:self.disaplayView];
    CGFloat height=0.0f;
    if (secondTitle.length == 0 && firstTitle.length > 0){
        height = 173*HEIGHT;
    }else{
        height = 193*HEIGHT;
    }
    [self loadThemeWithStr:imageName firstTitle:firstTitle secondTitle:secondTitle];
    [self addConstrainsOnAlertTintWithHeight:height];
    [self selectButtonNubers:operationNum];
    
    [[self.confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (self.delegateSignal) {
            [self.delegateSignal subscribeNext:nil];
        }
    }];
}

- (void)addConstrainsOnAlertTintWithHeight:(CGFloat)height{
    [self.disaplayView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.window).offset(NaviH + 120*HEIGHT);
        make.centerX.equalTo(self.window);
        make.width.mas_equalTo(200*WIDTH);
        make.height.mas_equalTo(height);
    }];
}

// 选择一个按钮还是两个按钮
- (void)selectButtonNubers:(NSInteger)number{
    if (number == 1) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"确认" forState:UIControlStateNormal];
        [button setTitleColor:KLColor_A1 forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [self.disaplayView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self.disaplayView);
            make.height.mas_equalTo(44*HEIGHT);
        }];
        self.confirmBtn = button;
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = KLColor_B3;
        [self.disaplayView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(button.mas_top);
            make.width.centerX.equalTo(self.disaplayView);
            make.height.mas_equalTo(1);
        }];
    }else if (number == 2){
        
        UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftbutton setTitle:@"取消" forState:UIControlStateNormal];
        [leftbutton setTitleColor:KLColor_B7 forState:UIControlStateNormal];
        [leftbutton setBackgroundImage:[UIImage createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [self.disaplayView addSubview:leftbutton];
        [[leftbutton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self cancel];
        }];
        [leftbutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.equalTo(self.disaplayView);
            make.height.mas_equalTo(44*HEIGHT);
            make.width.mas_equalTo(100*WIDTH);
        }];
        
        UIButton *rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightbutton setTitle:@"确认" forState:UIControlStateNormal];
        [rightbutton setTitleColor:KLColor_A1 forState:UIControlStateNormal];
        [rightbutton setBackgroundImage:[UIImage createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [self.disaplayView addSubview:rightbutton];
        [rightbutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(self.disaplayView);
            make.height.mas_equalTo(44*HEIGHT);
            make.width.mas_equalTo(100*WIDTH);
        }];
        self.confirmBtn = rightbutton;
        
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = KLColor_B3;
        [self.disaplayView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(leftbutton.mas_top);
            make.width.centerX.equalTo(self.disaplayView);
            make.height.mas_equalTo(1);
        }];
        
        UIView *middleLineView = [[UIView alloc]init];
        middleLineView.backgroundColor = KLColor_B3;
        [self.disaplayView addSubview:middleLineView];
        [middleLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.bottom.equalTo(self.disaplayView);
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(44*HEIGHT);
        }];
        
    }
}
// 加载标题
- (void)loadThemeWithStr:(NSString *)imageName firstTitle:(NSString *)firstTitle secondTitle:(NSString *)secondTitle{
    if (secondTitle.length == 0){
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:imageName];
        [self.disaplayView addSubview:imageView];
        [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.disaplayView);
            make.top.equalTo(self.disaplayView).offset(18*HEIGHT);
        }];
        
        
        UILabel *firstTitleLb = [[UILabel alloc]init];
        firstTitleLb.text = firstTitle;
        firstTitleLb.textColor = KLColor_B6;
        firstTitleLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
        [self.disaplayView addSubview:firstTitleLb];
        [firstTitleLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.disaplayView);
            make.top.equalTo(imageView.mas_bottom).offset(15*HEIGHT);
        }];
    }else{
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:imageName];
        [self.disaplayView addSubview:imageView];
        [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.disaplayView);
            make.top.equalTo(self.disaplayView).offset(18*HEIGHT);
        }];

        
        UILabel *firstTitleLb = [[UILabel alloc]init];
        firstTitleLb.text = firstTitle;
        firstTitleLb.textColor = KLColor_B6;
        firstTitleLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
        [self.disaplayView addSubview:firstTitleLb];
        [firstTitleLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.disaplayView);
            make.top.equalTo(imageView.mas_bottom).offset(15*HEIGHT);
        }];
        
        UILabel *secondTitleLb = [[UILabel alloc]init];
        secondTitleLb.text = secondTitle;
        secondTitleLb.textColor = KLColor_B6;
        secondTitleLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
        [self.disaplayView addSubview:secondTitleLb];
        [secondTitleLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.disaplayView);
            make.top.equalTo(firstTitleLb.mas_bottom).offset(2*HEIGHT);
        }];
    }
}


// lazyout
- (UIView *)disaplayView{
    if (!_disaplayView) {
        _disaplayView = [[UIView alloc]init];
        _disaplayView.backgroundColor = [UIColor whiteColor];
        
    }
    return _disaplayView;
}
-(UIView *)dropView
{
    if (!_dropView) {
        _dropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KLScreenW, KLScreenH)];
        _dropView.backgroundColor = [UIColor blackColor];
        _dropView.alpha = 0.6f;
    }
    return _dropView;
}

//
- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.disaplayView layoutIfNeeded];
    [self.disaplayView setRadiusWithValue:10*WIDTH];
}


//
- (void)cancel{
    [self removeFromSuperview];
    [self.dropView removeFromSuperview];
    [self.disaplayView removeFromSuperview];
}
@end
